package com.cqeec.myblog.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.cqeec.myblog.dao.ArticleDao;
import com.cqeec.myblog.dao.ArticleDaoImpl;
import com.cqeec.myblog.model.Article;
import com.cqeec.myblog.util.PageBean;

/**
 * 文章服务实现类(业务逻辑处理类)。
 * 
 * @author TLF。
 *
 */
public class ArticleServiceImpl implements ArticleService {
	ArticleDao articleDao = new ArticleDaoImpl();
	
	@Override
	public List<Article> getAllArticle() {
		// 创建文章数据访问对象赋给文章接口变量
		ArticleDao articleDao = new ArticleDaoImpl();
		// 调用方法获取所有文章对象列表
		List<Article> list = articleDao.select();
		// 遍历所有文章对象
		String content;
		Random r = new Random();
		for (Article a : list) {
			// 将文章内容随机的取出前部分+......
			content = a.getContent();
			content = content.substring(0, r.nextInt(content.length())) + "......";
			a.setContent(content);
		}
		return list;
	}

	@Override
	public void deleteArticleById(String articleId) {
		// TODO Auto-generated method stub
		articleDao.deleteArticleById(articleId);
	}

	@Override
	public void createArticle(Article article) {
		// TODO Auto-generated method stub
		articleDao.createArticle(article);
	}

	@Override
	public Article getArticleById(String articleId) {
		List<Article> articles=articleDao.select();
		Article article=null;
		for(Article temp:articles) {
			if(temp.getId().equals(articleId))article=temp;
		}
		return article;
	}

	@Override
	public void updateById(String id, String title, String content) {
		// TODO Auto-generated method stub
		articleDao.updateById(id,title,content);
	}

	@Override
	public PageBean<Article> findArticleDescribByCondition(String cid,Integer curPage) {
		PageBean<Article> pageBean=new PageBean<>();
			//当前页赋值
			pageBean.setCurPage(curPage);
			//如果cname为null则查询所有分类
			List<Article> list=articleDao.selectByCidAndCurPage(cid,pageBean);
			//查询相应文章的记录数
			int count=articleDao.selectCountByCid(cid);
			//赋值
			pageBean.setList(list);
			pageBean.setPageRecordCount(count);
			return pageBean;
	}

	

}
